package cn.com.hbhgkj.web.struts2;

import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import cn.com.hbhgkj.bussiness.UserService;
import cn.com.hbhgkj.bussiness.exception.DuplicateRegistException;
import cn.com.hbhgkj.bussiness.exception.LoginFailedException;
import cn.com.hbhgkj.bussiness.exception.PwdNotMatchingException;
import cn.com.hbhgkj.common.ServiceException;
import cn.com.hbhgkj.pojo.User;

/*
 * 
 * Author : Cody Dong
 * Date : 
 * 
 * */
public class UserAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public String userNameExist(){
		try {
			if(userService.isUserNameExist(user.getUserName())){
				this.success = false;
				this.message = "该用户名已经存在!";
			}else{
				this.success = true;
				this.message = "该用户名可用!";
			}
			
		} catch (ServiceException e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
		
	}
	
	public String nameExist(){
		try {
			if(userService.isCompanyNameExist(user.getCompanyName())){
				this.success = false;
				this.message = "该单位名称或姓名已经存在!";
			}else{
				this.success = true;
				this.message = "该单位名称或姓名可用!";
			}
			
		} catch (ServiceException e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
		
	}

	public String regist() {
		try {
			userService.regist(user);
			this.success = true;
			this.message = "注册成功！";
		} catch (DuplicateRegistException e) {
			this.success = false;
			this.message = e.getMessage();
		} catch (ServiceException e) {
			this.success = false;
			this.message = e.getMessage();
		}
		return SUCCESS;
	}

	public String login() {
		try {
			userService.login(user);

			this.cacheUser(user);

			this.success = true;
			this.message = "登陆成功！";
		} catch (LoginFailedException e) {
			this.success = false;
			this.message = e.getMessage();
		} catch (ServiceException e) {
			this.success = false;
			this.message = "登陆失败，服务器异常！";
		}
		return SUCCESS;
	}
	
	public String logout(){
		this.clearUserCache();
		this.success = true;
		return SUCCESS;
	}
	
	public String updateInfo(){
		try {
			user.setStatus(this.getLoggedInUser().getStatus());
			user.setId(getLoggedInUser().getId());
			user.setRegisterTime(getLoggedInUser().getRegisterTime());
			user.setReviewTime(getLoggedInUser().getReviewTime());
			user = userService.updateUserInfo(user);
			this.cacheUser(user);
			this.success = true;
			this.message = "修改成功！";
		} catch (PwdNotMatchingException e) {
			this.success = false;
			this.message = e.getMessage();
		} catch (ServiceException e) {
			this.success = false;
			this.message = "修改失败，服务器异常！";
		}
		return SUCCESS;
	}

	private User user;

	private List<User> listUser;

	public List<User> getListUser() {
		return listUser;
	}

	public void setListUser(List<User> listUser) {
		this.listUser = listUser;
	}

	private int reviewCode;

	public int getReviewCode() {
		return reviewCode;
	}

	public void setReviewCode(int reviewCode) {
		this.reviewCode = reviewCode;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	private UserService userService;

	@JSON(serialize = false)
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
